<?php
/**
 * @file
 * @ingroup SF
 */

class SFQueryAPI extends ApiBase {

    public function __construct( $query, $moduleName ) {
        parent::__construct( $query, $moduleName );
    }

    public function execute() {
        $params = $this->extractRequestParams();
        $query = $params['query'];

        if ( is_null( $query ) || strlen( $query ) == 0 ) {
            $this->dieUsage( 'The $query must be specified', 'param_query' );
        }

        $data = array();
        $data = SFUtils::getAllPagesForQuery( $query );

        // Format data as the API requires it.
        $formattedData = array();
        foreach ( $data as $value ) {
            $formattedData[] = array( 'title' => str_replace('_',' ',$value->getDbKey()) );
        }

        // Set top-level elements.
        $result = $this->getResult();
        $result->setIndexedTagName( $formattedData, 'p' );
        $result->addValue( null, $this->getModuleName(), $formattedData );
    }

    protected function getAllowedParams() {
        return array (
            'query' => null,
        );
    }

    protected function getParamDescription() {
        return array (
            'query' => 'Search substring',
        );
    }

    protected function getDescription() {
        return 'Semantic query api call used by the Semantic Forms extension (http://www.mediawiki.org/Extension:Semantic_Forms)';
    }

    protected function getExamples() {
        return array (
            'api.php?action=sfqueryapi&query=[[Category:+]]',
        );
    }

    public function getVersion() {
        return __CLASS__ . ': $Id: SF_QueryAPI.php 112139 2012-10-05 19:40:49Z vedmaka $';
    }

}
